package com.hx.localmiaosha.service.imp;

import com.hx.common.api.MiaoshaUserService;
import com.hx.common.domain.MiaoshaUser;
import com.hx.common.exception.GlobleException;
import com.hx.common.utils.MD5Utils;
import com.hx.common.utils.UUIDUtil;
import com.hx.common.vo.LoginVo;

import com.hx.localmiaosha.dao.LocalMiaoShaUserDao;
import com.hx.localmiaosha.service.LocalMiaoshaUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

import static com.hx.common.common.enums.ResultStatus.SYSTEM_ERROR;

/**
 * @Author wonghiu
 * @Date 2021/6/4 15:30
 * @Version 1.0.1
 */
@Component
public class LocalMiaoshaUserServiceImp implements LocalMiaoshaUserService {
    public static final String COOKIE_NAME_TOKEN = "token" ;
    private static Logger logger = LoggerFactory.getLogger(MiaoshaUserService.class);
//    @Autowired
//    private RedisService redisService;

    @Autowired
    private LocalMiaoShaUserDao miaoShaUserDao;

    @Override
    public MiaoshaUser login(LoginVo loginVo) {
        if(loginVo ==null){
            throw  new GlobleException(SYSTEM_ERROR);
        }

        String mobile =loginVo.getMobile();
        String password =loginVo.getPassword();
        MiaoshaUser user = getByNickName(mobile);
        if(user == null) {
//            throw new GlobleException(MOBILE_NOT_EXIST);
            return null;
        }

        String dbPass = user.getPassword();
        String saltDb = user.getSalt();
        String calcPass = MD5Utils.formPassToDBPass(password,saltDb);
        if(!calcPass.equals(dbPass)){
//            throw new GlobleException(PASSWORD_ERROR);
            return null;
        }
        //生成cookie 将session返回游览器 分布式session
//        String token= UUIDUtil.uuid();
//        addCookie(response, token, user);
//        return true ;
        return user;
    }

    @Override
    public boolean register(HttpServletResponse response , String userName , String passWord , String salt) {
        MiaoshaUser miaoShaUser =  new MiaoshaUser();
        miaoShaUser.setNickname(userName);
        String DBPassWord =  MD5Utils.formPassToDBPass(passWord , salt);
        miaoShaUser.setPassword(DBPassWord);
        miaoShaUser.setRegisterDate(new Date());
        miaoShaUser.setSalt(salt);
        miaoShaUser.setNickname(userName);
        try {
            miaoShaUserDao.insertMiaoShaUser(miaoShaUser);
            MiaoshaUser user = miaoShaUserDao.getByNickname(miaoShaUser.getNickname());
            if(user == null){
                return false;
            }
            //生成cookie 将session返回游览器 分布式session
            String token= UUIDUtil.uuid();
            addCookie(response, token, user);
        } catch (Exception e) {
            logger.error("注册失败",e);
            throw e;
        }
        return true;
    }

    @Override
    public MiaoshaUser getUserById(Long userId) {
        return miaoShaUserDao.getById(userId);
    }
    @Override
    public MiaoshaUser getUserByNickName(String nickName) {
        return miaoShaUserDao.getByNickName(nickName);
    }
    public MiaoshaUser getByNickName(String nickName) {
//        //取缓存
//        MiaoshaUser user = redisService.get(MiaoShaUserKey.getByNickName, ""+nickName, MiaoshaUser.class);
//        if(user != null) {
//            return user;
//        }
        //取数据库
        MiaoshaUser user = miaoShaUserDao.getByNickname(nickName);
//        if(user != null) {
//            redisService.set(MiaoShaUserKey.getByNickName, ""+nickName, user);
//        }
        return user;
    }
    private void addCookie(HttpServletResponse response, String token, MiaoshaUser user) {
//        redisService.set(MiaoShaUserKey.token, token, user);
        Cookie cookie = new Cookie(COOKIE_NAME_TOKEN, token);
        //设置有效期
//        cookie.setMaxAge(MiaoShaUserKey.token.expireSeconds());
        cookie.setPath("/");
        response.addCookie(cookie);
    }

}
